﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using Seagull.BarTender.Print;
using System.IO;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;
using DXApplicationTangche.Utils;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form裁剪贴 : DevExpress.XtraEditors.XtraForm
    {
        private OrderCommitDTO返回 orderCommitDTO = new OrderCommitDTO返回();
        public Form裁剪贴()
        {
            InitializeComponent();
            if (!File.Exists(@"tag\tailor.btw"))
            {
                FileUtil.DownloadPicture(URL.图片url + URL.打印模板路径 + "tailor.btw", @"tag\" + "tailor.btw", -1);
            }
            this.ActiveControl = this.textBox扫描批号;
        }

        private void textBox扫描批号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox扫描批号.Text))
                {
                    MessageBox.Show("请扫描生产批号");
                    return;
                }
                try
                {
                    this.orderCommitDTO = 订单预览新Service.get订单预览By批号(this.textBox扫描批号.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("无法查到订单");
                    return;
                }
                this.textBox订单号.Text = this.orderCommitDTO.orderNo;
                for (int i = 1; i < 24; i=i+2)
                {
                    this.print(i);
                }
                this.textBox扫描批号.Clear();
                this.textBox订单号.Clear();
                this.ActiveControl = this.textBox扫描批号;
            }
        }
        private void print(int i)
        {
            Engine btEngine = new Engine();
            btEngine.Start();
            LabelFormatDocument btFormat = btEngine.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory + @"tag/tailor.btw");
            //LabelFormatDocument btFormat = btEngine.Documents.Open("C:\\裁剪.btw");
            //指定打印机名 
            //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            btFormat.PrintSetup.PrinterName = "POSTEK G-3106 (裁剪贴)";
            //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
            //btFormat.PrintSetup.PrinterName = "PDFCreator";
            //打印份数                   
            btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
            //改变标签打印数份连载 
            btFormat.PrintSetup.NumberOfSerializedLabels = 1;
            //对BTW模版相应字段进行赋值 

            btFormat.SubStrings["orderNo"].Value = this.orderCommitDTO.orderNo;
            btFormat.SubStrings["no"].Value = i.ToString();
            btFormat.SubStrings["batchNo1"].Value = this.textBox扫描批号.Text.Trim();
            btFormat.SubStrings["orderNo2"].Value = this.orderCommitDTO.orderNo;
            btFormat.SubStrings["no2"].Value = (i+1).ToString();
            btFormat.SubStrings["batchNo2"].Value = this.textBox扫描批号.Text.Trim();

            Messages messages;
            int waitout = 10000; // 10秒 超时 
            Seagull.BarTender.Print.Result nResult1 = btFormat.Print("裁剪贴" + this.textBox扫描批号.Text, waitout, out messages);
            btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
            //不保存对打开模板的修改 
            btFormat.Close(SaveOptions.DoNotSaveChanges);
            //结束打印引擎                  
            btEngine.Stop();
        }
    }
}